#!/usr/bin/env python


import roslib; roslib.load_manifest('knobs_rev')
import rospy


from smach import State, StateMachine

from PySide.QtCore import * 
from PySide.QtGui import * 

import sys


class Click(State):
    def __init__(self, group_name, buttons):
        State.__init__(self, outcomes=['succeeded', 'failed'])
        self.layout = QVBoxLayout()

        self.button_list = [QPushButton(name) for name in buttons]
        self.return_list = [value for value in buttons.values()]
        map(lambda x: x[0].clicked.connect(lambda: self.callback(x[1])), zip(self.button_list, self.return_list))
        map(self.layout.addWidget, self.button_list)

        self.group = QGroupBox(group_name)
        self.group.setLayout(self.layout)

    def callback(self, value):
        self.return_value = value
        self.group.hide()
        app.quit()

    def execute(self, userdata):
        self.return_value = None

        self.group.show()
        app.exec_()

        return self.return_value


if __name__ == '__main__':
    rospy.init_node('click')

    app = QApplication(sys.argv)

    machine = StateMachine(outcomes=['succeeded', 'failed'])
    with machine:
        StateMachine.add('CLICK_1', Click('One', {'succeeded':'succeeded', 'failed':'failed'}), transitions={'succeeded':'CLICK_2'})
        StateMachine.add('CLICK_2', Click('Two', {'succeeded':'succeeded', 'failed':'failed'}), transitions={'succeeded':'CLICK_1'})

    machine.execute()

#    rospy.spin()
